package com.yanqu.road.server.cmd.newmonopolymkt;

import com.yanqu.road.dao.impl.nenmonopolymarket.NewMonopolyMarketUserDataDaoImpl;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.NewMonopolyMarketUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_SEND_OFFICE_END_TIME_FROM_CROSS, desc = "跨服自动发奖(榷署周结算)")
public class NewMonopolyMarketSendOfficeSettleRewardCmdFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        NewMonopolyMarketProto.NewMonopolyMarketSendOfficeSettleRewardSync sync = NewMonopolyMarketProto.NewMonopolyMarketSendOfficeSettleRewardSync.parseFrom(yanQuMessage.getBytes());
        for (NewMonopolyMarketProto.NewMonopolyMarketSendOfficeSettleTemp temp : sync.getOfficeSettleList()) {
            GamePlayer gamePlayer = GamePlayerMgr.getOnlinePlayer(temp.getUserId());
            if(gamePlayer != null){
                NewMonopolyMarketUserData userData = gamePlayer.getModule(NewMonopolyMarketModule.class).userData;
                if(userData != null){
                    NewMonopolyMarketMgr.refreshOfficeTeaReward(userData, temp.getEndTime(), temp.getOfficeNo(), temp.getOfficeLevel());
                    userData.setTeaLastGetTime(0);
                    NewMonopolyMarketMgr.getLogger().info("新榷场 结算待客茶  玩家 {}，位置 {}，等级 {}，结算时间 {}", temp.getUserId(), temp.getOfficeNo(), temp.getOfficeLevel(), temp.getEndTime());
                }
            }else {
                NewMonopolyMarketUserData userData = new NewMonopolyMarketUserDataDaoImpl().getNewMonopolyMarketUserData(temp.getUserId());
                if(userData != null){
                    NewMonopolyMarketMgr.refreshOfficeTeaReward(userData, temp.getEndTime(), temp.getOfficeNo(), temp.getOfficeLevel());
                    userData.setTeaLastGetTime(0);
                    NewMonopolyMarketMgr.getLogger().info("新榷场 结算待客茶  [离线]玩家 {}，位置 {}，等级 {}，结算时间 {}", temp.getUserId(), temp.getOfficeNo(), temp.getOfficeLevel(), temp.getEndTime());
                    new NewMonopolyMarketUserDataDaoImpl().update(userData);
                }

            }
        }
    }
}
